<template>
  <el-dialog title="请填写信息" :visible="dialogVisible" width="20%">
    <el-form :model="form" label-position="right" label-width="100px" style="padding-right: 40px">
      <el-form-item label="用户名">
        <el-input size="small" v-model="form.userName" placeholder="请输入用户名"></el-input>
      </el-form-item>
      <el-form-item label="性别">
        <el-radio-group size="small" v-model="form.gender">
          <el-radio label="男"></el-radio>
          <el-radio label="女"></el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="年龄">
        <el-input size="small" v-model="form.age" placeholder="请输入年龄"></el-input>
      </el-form-item>
      <el-form-item label="电话">
        <el-input size="small" v-model="form.phone" placeholder="请输入手机号"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button size="small" @click="close">取 消</el-button>
      <el-button size="small" type="primary" @click="save">保 存</el-button>
    </div>
  </el-dialog>
</template>

<script>
import request from "@/utils/request";
  export default{
    props:{
      dialogVisible:Boolean,
      form:Object,
    },
    data(){
      return{

      }
    },methods:{
      close(){
        this.$emit('close',false);
      },
      save(){
        //如果没有id 走新增接口
        if (!this.form.id){
          //post请求把form对象传到后端，后端写逻辑把form存到数据库里
          request.post("/account/addadmin", this.form).then(res => {
            //如果接口调用成功，则把模态框关闭，重新加载一下数据库的数据
            if (res.code === '0') {
              this.$notify.success("新增成功");
              this.close();
            } else {
              this.$notify.error(res.msg); // 如果不成功，返回报错信息
            }
          });
        }
        // 如果有id，走修改接口
        else {
          request.post("/account/addadmin", this.form).then(res => {
            //如果接口调用成功，则把模态框关闭，重新加载一下数据库的数据
            if (res.code === '0') {
              this.$notify.success("修改成功");
              this.close();
            } else {
              this.$notify.error(res.msg); // 如果不成功，返回报错信息
            }
          });
        }
      }
    }
  }
</script>


<style scoped>

</style>